<?php
/*+--------------------------------------------------------------------
 * 作	  者:songtt
 +----------------------------------------------------------------------
 * 创建日期：2016-09-08
 +----------------------------------------------------------------------
 * 文件描述：系统选项
 +----------------------------------------------------------------------
 * 升级记录：
 +----------------------------------------------------------------------
*/

class QuotaAction extends CommonAction{
	/**
	 * 显示页面
	 */
	public function index() {
		$model = new QuotaModel();
			// 条件检索
		if(!empty($_REQUEST['search_keyword'])) {
			$where['code|value'] = array('like', '%'.$this->_request('search_keyword').'%');
		}
		//分页处理
		import('ORG.Util.Page');// 导入分页类
		$count = $model->where($where)->count();  // 查询满足要求的总记录数
		
		$p = $this->_page($model, 'update_date', 'desc');  //分页所需信息
		$page = new Page($count, $p['listRows']);  // 实例化分页类 传入总记录数和每页显示的记录数
		
		$data = $model->field("id,
							   get_field_dict_name('quota','first_quota',first_quota) first_quota,
							   get_field_dict_name('quota','second_quota',second_quota) second_quota,
							   get_field_dict_name('quota','third_quota',third_quota) third_quota,
							   get_field_dict_name('quota','stage',stage) stage,
							   code,
							   value,
							   quota_remark,
							   get_user_name(operator) operator,
			 				   get_formate_date(update_date) update_date")
				
					 /* ->order("`".$p['order']."` ".$p['sort']) */
					 ->order('code','desc')
					 ->limit($page->firstRow.', '.$page->listRows)
					 ->select();
		$this->assign('list',$data);
		//dump($data); 
		$show = $page->show();	//分页显示输出
		$this->assign('show', $show);	//绑定分页显示字符串
		$this->display();
	}
	
	/**
	 * 显示添加页面
	 */
	public function add() {		
		$this->assign('first_quota_list',set_field_dict_list('quota','first_quota'));
		$this->assign('second_quota_list',set_field_dict_list('quota','second_quota'));
		$this->assign('third_quota_list',set_field_dict_list('quota','third_quota'));
		$this->assign('stage_list',set_field_dict_list('quota','stage'));
		$this->display();
	}
	
	/**
	 * 将添加的信息写入数据库
	 */
	public function insert() {
		$model = new QuotaModel();
		$data = $model->create();
		if(false !== $data){
			if(false !== $model->add()){
				$this->success('添加成功', __APP__.'?g=Admin&m=Quota&a=index');
			} else {
				$this->error('添加失败');
			}
		} else {
			$this->error($model->getError());
		}
	}
	
	/**
	 * 显示修改页面
	 */
	public function edit() {
		$id = $_REQUEST['id'];
		$model = new QuotaModel();
		$system_option = new SystemOptionModel();
		$data = $model->field("id,
							   first_quota,
							   second_quota,
							   third_quota,
							   stage,
							   code,
							   value,
							   quota_remark")
					  ->where("id = '".$id."'")
					  ->find();
		$this->assign('first_quota_list',set_field_dict_list('quota','first_quota'));
		$this->assign('second_quota_list',set_field_dict_list('quota','second_quota'));
		$this->assign('stage_list',set_field_dict_list('quota','stage'));
		$this->assign('third_quota_list',set_field_dict_list('quota','third_quota'));
		$all_score = $system_option->where("name='问卷总分'")->getField("value");
		//查询分数
		$remark_first = $this->get_field_dict_remark("quota",$data['first_quota']);
		$remark_second = $this->get_field_dict_remark("quota",$data['second_quota']);
		$remark_third = $this->get_field_dict_remark("quota",$data['third_quota']);
		//一级指标分数
		$first_score = sprintf("%.2f",$all_score*$remark_first);
		//dump($first_score);
		//二级指标分数
		//$second_quota_remark_all = $this->get_second_quota_remark_all($data['first_quota']);
		//$second_quota_list = $model->field("second_quota")->where("third_quota='".$data['third_quota']."'")->group("second_quota")->find();
		$second_quta_score = sprintf("%.2f",$first_score*($remark_second/$remark_first));
		//dump($second_quta_score);
		//三级分数
		$third_quta_score = sprintf("%.2f",$second_quta_score*($remark_third/$remark_second));
		//dump($third_quta_score);
		$this->assign('third_quta_score',$third_quta_score);
		$this->assign('list',$data);
		$this->display();
	}
	
	/**
	 * 将修改的信息更新到数据库
	 */
	public function update() {
		$model = new QuotaModel();
		$data = $model->create();
		if(false !== $data){
			$data = $model->save();
			if(false !== $data){
				$this->success('修改成功', __APP__.'?g=Admin&m=Quota&a=index');
			} else {
				$this->error('修改失败');
			}
		} else {
			$this->error($model->getError());
		}
	}
	
	/**
	 * 删除信息
	 */
	public function delete() {
		$model = new QuotaModel();
		$ids = $_REQUEST['ids'];
		$id = implode("','", $ids);	//用','分割id数组，以便于拼装sql语句
		$id = "'".$id."'";
		if(true == $model->where('id in('.$id.')')->delete()) {
			redirect(__APP__.'?g=Admin&m=Quota&a=index');
		} else {
			$this->error('删除失败！');
		}
	}
}

?>